﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PackBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private PubParasInfo pubParasInfo;

        public PackBLL()
        {
            this.pubParasInfo = null;
        }

        public PackBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PackBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> packIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PackInfo> packInfos = new List<PackInfo>();
                PackInfo packInfo = null;
                foreach (string id in packIDS)
                {
                    packInfo = packDA.GetPackByID(dataBase, tran, id, this.dataBaseID[0]);
                    packInfos.Add(packInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (packDA.DeletePack(packIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < packIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PackInfo>(dataBase, tran, packInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PACK", this.dataBaseID[0]);
                        temp = temp + packIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return packDA.GetAllPack(this.dataBaseID[0]);
        }

        public object GetInfoByID(string packID)
        {
            return packDA.GetPackByID(packID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            PackQueryEntity packQuery = obj as PackQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(packQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + packQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(packQuery.PackID))
            {
                sqlWhere.Add(" PACK_ID " + packQuery.PackID);
            }
            if (!string.IsNullOrEmpty(packQuery.Description))
            {
                sqlWhere.Add(" DESCRIPTION " + packQuery.Description);
            }
            return packDA.GetPackByQueryList(sqlWhere, packQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string packID = string.Empty;
            try
            {
                PackInfo pack = obj as PackInfo;
                if (packDA.CheckPackIDUnique(pack.PackID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2220_001", this.pubParasInfo.UserLanguage, new List<string> { pack.PackID });
                }
                pack.UpdatedBy = pack.CreatedBy;
                pack.CreatedDate = DateTime.Now;
                pack.UpdatedDate = DateTime.Now;
                if (packDA.InsertPack(pack, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PackInfo>(null, pack, AuditAction.Insert, this.pubParasInfo.UserID, "PACK", this.dataBaseID[0]);
                    packID = pack.PackID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return packID;
        }

        public string Update(object obj)
        {
            string packID = string.Empty;
            try
            {
                PackInfo pack = obj as PackInfo;
                pack.UpdatedDate = DateTime.Now;
                PackInfo oldPackInfo = packDA.GetPackByID(pack.PackID, this.dataBaseID[0]);
                if (packDA.UpdatePack(pack, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PackInfo>(oldPackInfo, pack, AuditAction.Update, this.pubParasInfo.UserID, "PACK", this.dataBaseID[0]);
                    packID = pack.PackID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return packID;
        }
    }
}

